<template>
  <DaprPlusTable
    :table-title="$t('fsb.softwares.function.list')"
    :entity="entity"
    :grid-options="gridOptions"
    :query-api="crudApi.getPage"
    :create-api="crudApi.create"
    :update-api="crudApi.update"
    :delete-api="crudApi.delete"
    :default-new-record="createDefaultFunction"
    @update:grid-api="onGridApiUpdate"
  />
</template>

<script lang="ts" setup>
import type { ExtendedVxeGridApi, VxeGridProps } from '#/adapter/vxe-table';
import { DaprPlusTable } from '#/components/daprplus';
import { createCrudApi } from '#/api/daprplus';
import type { IInDto, IOutDto, EntityInfo } from '#/utils/daprplus';
import { createBooleanColumn } from '#/utils/daprplus';
import { $t } from '#/locales';

const entity: EntityInfo = {
  code: 'Function',
  display: $t('fsb.softwares.function.title'),
};

enum ClientFunctionType {
  Service = 0,
  DiDaily = 1,
  SkyDaily = 2,
  Buss = 3,
  LongTime = 4,
  Other = 5,
  Admin = 6,
}

/**
 * 功能类型标签
 */
const functionTypeLabels: Record<ClientFunctionType, string> = {
  [ClientFunctionType.Service]: '服务功能',
  [ClientFunctionType.DiDaily]: '人间日常',
  [ClientFunctionType.SkyDaily]: '仙魔日常',
  [ClientFunctionType.Buss]: '商业功能',
  [ClientFunctionType.LongTime]: '长时功能',
  [ClientFunctionType.Other]: '其他功能',
  [ClientFunctionType.Admin]: '管理功能',
};

const baseUrl = '/softwares/api/function';
const crudApi = createCrudApi<FunctionInDto, FunctionOutDto>(baseUrl);

// 功能类型选项
const functionTypeOptions = Object.entries(ClientFunctionType)
  .filter(([key]) => isNaN(Number(key)))
  .map(([key, value]) => ({
    value,
    label: functionTypeLabels[value as ClientFunctionType],
  }));

// 验证规则
const validRules = {
  name: [
    { required: true, message: '功能名称不能为空' },
    { min: 2, max: 30, message: '功能名称长度应在2-30个字符之间' },
  ],
  beginLevel: [
    { required: true, message: '开始等级不能为空' },
    { min: 1, message: '开始等级必须大于0' },
  ],
  needVersion: [
    { required: true, message: '版本号不能为空' },
    { max: 20, message: '版本号长度应在0-20个字符之间' },
  ],
  remark: [{ max: 500, message: '备注长度应在0-500个字符之间' }],
};

// 扩展配置
const gridOptions: VxeGridProps<FunctionOutDto> = {
  editRules: validRules,
  columns: [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      sortable: true,
      width: 130,
    },
    {
      field: 'name',
      title: '功能名称',
      sortable: true,
      align: 'left',
      minWidth: 150,
      editRender: { name: 'input' },
    },
    {
      field: 'clientFunctionType',
      title: '功能类型',
      sortable: true,
      width: 120,
      editRender: {
        name: 'select',
        options: functionTypeOptions,
      },
      formatter: ({ cellValue }) => {
        return functionTypeLabels[cellValue as ClientFunctionType] || cellValue;
      },
    },
    {
      field: 'beginLevel',
      title: '开始等级',
      sortable: true,
      width: 122,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'endLevel',
      title: '结束等级',
      sortable: true,
      width: 120,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    createBooleanColumn('isSub', '子功能', { width: 100 }),
    createBooleanColumn('isSky', '仙魔', { width: 85 }),
    createBooleanColumn('canSell', '上架', { width: 85 }),
    createBooleanColumn('isCircle', '循环', { width: 85 }),
    createBooleanColumn('isLocked', '已锁定', { width: 100 }),
    {
      field: 'price',
      title: '价格',
      sortable: true,
      width: 100,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'needVersion',
      title: '版本号',
      sortable: true,
      width: 110,
      editRender: { name: 'input' },
    },
    {
      field: 'extendData',
      title: '扩展数据',
      align: 'left',
      visible: false,
      minWidth: 200,
      editRender: { name: 'input' },
    },
    {
      field: 'remark',
      title: '备注说明',
      align: 'left',
      visible: false,
      minWidth: 100,
      editRender: { name: 'input' },
    },
    {
      field: 'createdTime',
      title: '创建时间',
      formatter: 'formatDateTime',
      width: 150,
    },
  ],
};

let gridApi: ExtendedVxeGridApi;

function onGridApiUpdate(api: ExtendedVxeGridApi) {
  gridApi = api;
}

function createDefaultFunction(): FunctionInDto {
  return {
    id: '0',
    name: '新功能',
    beginLevel: 1,
    endLevel: undefined,
    isSub: false,
    isCircle: false,
    isSky: false,
    canSell: false,
    price: 0,
    extendData: '',
    remark: '',
    needVersion: '1.0.0',
    clientFunctionType: 0,
    isLocked: false,
  };
}

export interface FunctionInDto extends IInDto {
  name: string;
  beginLevel: number;
  endLevel?: number;
  isSub: boolean;
  isCircle: boolean;
  isSky: boolean;
  canSell: boolean;
  price: number;
  extendData?: string;
  remark?: string;
  needVersion: string;
  clientFunctionType: ClientFunctionType;
  isLocked: boolean;
}

export interface FunctionOutDto extends FunctionInDto, IOutDto {
  createdTime: string;
}
</script>

<style scoped></style>
